package com.woniuxy.carlife.util;

import com.woniuxy.carlife.service.CouponService;
import com.woniuxy.carlife.service.ServiceInfoService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author fx
 * @ClassName:ServiceScheduler
 * @Description: 定时器类，定时执行任务
 * @date 2021/4/8 19:13
 **/

@Component
public class ServiceScheduler {
    //private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    @Resource
    private CouponService couponService;

    @Resource
    private ServiceInfoService infoService;

    //每隔2秒执行一次
//    @Scheduled(fixedRate = 2000)
//    public void doWaitSeconds() {
//        System.out.println("定时任务执行时间：" + new Timestamp(new Date().getTime()));
//    }

    //每天3：05执行  秒、分、时、日、月、星期、年
    @Scheduled(cron = "30 29 16 ? * *")
    public void doCertainPoints() {
        System.out.println("执行定时方法");
        System.out.println(new Timestamp(new Date().getTime()));
//        System.out.println("定时任务执行时间：" + dateFormat.format(new Date()));
        //修改优惠券库存
        boolean b = couponService.updateCouponNumByCouponIdToMySql();
        if (b){
            System.out.println("修改优惠券库存执行完成");
        }else{
            System.out.println("修改优惠券库存执行失败");
        }
        //修改点赞量、收藏量等
        Boolean aBoolean = infoService.updateServcieInfoCollectByServiceInfoIdToSql();
        if (aBoolean){
            System.out.println("修改点赞量、收藏量等执行完成");
        }else{
            System.out.println("修改点赞量、收藏量等执行失败");
        }
        System.out.println("定时方法执行完成");
        System.out.println(new Timestamp(new Date().getTime()));
    }
}
